package com.gopay.common.cps.dao.gsrt.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.gsrt.GsrtLoanDisbursReturnedSubDao;
import com.gopay.common.domain.gsrt.GsrtLoanDisbursReturnedSub;

/**
 * 
 * @Description: 分账明细表  放款和回款公用表
 * @author hougn
 * @date 2016年1月27日 上午10:47:51 
 *
 */
@Repository("gsrtLoanDisbursReturnedSubDao")
public class GsrtLoanDisbursReturnedSubDaoImpl extends BaseDAOImpl<GsrtLoanDisbursReturnedSub, Serializable>
        implements GsrtLoanDisbursReturnedSubDao {

    @Override
    public List<GsrtLoanDisbursReturnedSub> findByParentDsRnIdAndTxnStaCd(Long parentDsRnId, int txnStaCd) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GsrtLoanDisbursReturnedSub.class);
        criteria.add(Restrictions.eq("parentDsRnId", parentDsRnId));
        criteria.add(Restrictions.eq("txnStaCd", txnStaCd));
        List<GsrtLoanDisbursReturnedSub> list = this.findByCriteria(criteria);
        return list;
    }
    
    @Override
    public List<GsrtLoanDisbursReturnedSub> findAllByParentDsRnId(Long parentDsRnId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GsrtLoanDisbursReturnedSub.class);
        criteria.add(Restrictions.eq("parentDsRnId", parentDsRnId));
        List<GsrtLoanDisbursReturnedSub> list = this.findByCriteria(criteria);
        return list;
    }

    /**
     *  
     * @param merCustId
     * @param merOrderId
     * @param type  业务类别  放款01 ; 回款02
     * @return
     */
    @Override
    public GsrtLoanDisbursReturnedSub getBymerOrderId(String merCustId, String merOrderId, String type) {
        String hql = "from GsrtLoanDisbursReturnedSub where  merCustId='" + merCustId + "' and merOrderId='"
                + merOrderId + "' and type='" + type + "'";
        List<GsrtLoanDisbursReturnedSub> list = this.findByHql(hql);
        return (GsrtLoanDisbursReturnedSub) list.get(0);
    }

    @Override
    public boolean findByMerCustIdAndMerOrderIdIn(String merCustId, String type, List<String> merOrderId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GsrtLoanDisbursReturnedSub.class);
        criteria.add(Restrictions.in("merOrderId", merOrderId));
        criteria.add(Restrictions.eq("merCustId", merCustId));
        criteria.add(Restrictions.eq("type", type));
        return this.findCountByCriteria(criteria) > 0;
    }
    
}
